<template>
  <div>
    <h2 id="page-heading" data-cy="CourseHeading">
      <span id="course-heading">Courses</span>
      <div class="d-flex justify-content-end">
        <button class="btn btn-info mr-2" v-on:click="handleSyncList" :disabled="isFetching">
          <font-awesome-icon icon="sync" :spin="isFetching"></font-awesome-icon> <span>Refresh List</span>
        </button>
        <router-link :to="{ name: 'CourseCreate' }" custom v-slot="{ navigate }">
          <button
            @click="navigate"
            id="jh-create-entity"
            data-cy="entityCreateButton"
            class="btn btn-primary jh-create-entity create-course"
          >
            <font-awesome-icon icon="plus"></font-awesome-icon>
            <span> Create a new Course </span>
          </button>
        </router-link>
      </div>
    </h2>
    <br />
    <div class="alert alert-warning" v-if="!isFetching && courses && courses.length === 0">
      <span>No courses found</span>
    </div>
    <div class="table-responsive" v-if="courses && courses.length > 0">
      <table class="table table-striped" aria-describedby="courses">
        <thead>
          <tr>
            <th scope="row" v-on:click="changeOrder('id')">
              <span>ID</span> <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'id'"></jhi-sort-indicator>
            </th>
            <th scope="row" v-on:click="changeOrder('courseCode')">
              <span>Course Code</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'courseCode'"></jhi-sort-indicator>
            </th>
            <th scope="row" v-on:click="changeOrder('courseName')">
              <span>Course Name</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'courseName'"></jhi-sort-indicator>
            </th>
            <th scope="row"></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="course in courses" :key="course.id" data-cy="entityTable">
            <td>
              <router-link :to="{ name: 'CourseView', params: { courseId: course.id } }">{{ course.id }}</router-link>
            </td>
            <td>{{ course.courseCode }}</td>
            <td>{{ course.courseName }}</td>
            <td class="text-right">
              <div class="btn-group">
                <router-link :to="{ name: 'CourseView', params: { courseId: course.id } }" custom v-slot="{ navigate }">
                  <button @click="navigate" class="btn btn-info btn-sm details" data-cy="entityDetailsButton">
                    <font-awesome-icon icon="eye"></font-awesome-icon>
                    <span class="d-none d-md-inline">View</span>
                  </button>
                </router-link>
                <router-link :to="{ name: 'CourseEdit', params: { courseId: course.id } }" custom v-slot="{ navigate }">
                  <button @click="navigate" class="btn btn-primary btn-sm edit" data-cy="entityEditButton">
                    <font-awesome-icon icon="pencil-alt"></font-awesome-icon>
                    <span class="d-none d-md-inline">Edit</span>
                  </button>
                </router-link>
                <b-button
                  v-on:click="prepareRemove(course)"
                  variant="danger"
                  class="btn btn-sm"
                  data-cy="entityDeleteButton"
                  v-b-modal.removeEntity
                >
                  <font-awesome-icon icon="times"></font-awesome-icon>
                  <span class="d-none d-md-inline">Delete</span>
                </b-button>
              </div>
            </td>
          </tr>
        </tbody>
        <infinite-loading
          ref="infiniteLoading"
          v-if="totalItems > itemsPerPage"
          :identifier="infiniteId"
          slot="append"
          @infinite="loadMore"
          force-use-infinite-wrapper=".el-table__body-wrapper"
          :distance="20"
        >
        </infinite-loading>
      </table>
    </div>
    <b-modal ref="removeEntity" id="removeEntity">
      <span slot="modal-title"
        ><span id="myapplication1App.course.delete.question" data-cy="courseDeleteDialogHeading">Confirm delete operation</span></span
      >
      <div class="modal-body">
        <p id="jhi-delete-course-heading">Are you sure you want to delete this Course?</p>
      </div>
      <div slot="modal-footer">
        <button type="button" class="btn btn-secondary" v-on:click="closeDialog()">Cancel</button>
        <button
          type="button"
          class="btn btn-primary"
          id="jhi-confirm-delete-course"
          data-cy="entityConfirmDeleteButton"
          v-on:click="removeCourse()"
        >
          Delete
        </button>
      </div>
    </b-modal>
  </div>
</template>

<script lang="ts" src="./course.component.ts"></script>
